/*
	The MIT License

	Copyright (c) 2010 IFMO/GameDev Studio

	Permission is hereby granted, free of charge, to any person obtaining a copy
	of this software and associated documentation files (the "Software"), to deal
	in the Software without restriction, including without limitation the rights
	to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
	copies of the Software, and to permit persons to whom the Software is
	furnished to do so, subject to the following conditions:

	The above copyright notice and this permission notice shall be included in
	all copies or substantial portions of the Software.

	THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
	IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
	FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
	AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
	LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
	OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
	THE SOFTWARE.
*/

#include "game_local.h"

/*-----------------------------------------------------------------------------
	Game main module :
-----------------------------------------------------------------------------*/

//
//	EGEntity::EGEntity
//
EGEntity::EGEntity( uint id )
{
	this->id	=	id;
	rigidbody.mass	=	0;

	//	create CDT :	
	lua_State *L = CoreLua();
	
	LUA_INTERFACE(L);
	
	lua_pushnil(L);
	table_ref = luaL_ref(L, LUA_REGISTRYINDEX);
}


//
//	EGEntity::~EGEntity
//
EGEntity::~EGEntity( void )
{
	lua_State *L = CoreLua();
	luaL_unref(L, LUA_REGISTRYINDEX, table_ref);
}
   

//
//	EGEntity::Update
//
void EGEntity::Update( uint dtime )
{
	//	get pose of physical body :
	if (rigidbody.entity) {
		rigidbody.entity->GetPose( position, orient );
	}

	if (character.character) {
		character.character->GetPose( position, orient );
	}
	
	if (ship.ship) {
		ship.ship->Update( dtime / 1000.0f );
		ship.ship->GetPose( position, orient );
	}
	
	//	set pose of view model :
	if (rendmodel) {
		rendmodel->SetPose( position, orient );
		rendmodel->ProcessAnimations( dtime / 1000.0f );
	}
}


//
//	EGEntity::UpdateScript
//
void EGEntity::UpdateScript( uint dtime )
{
	lua_State *L = CoreLua();
	try {
		lua_getglobal(L, "cgame");
		lua_getfield(L, -1, "entityFrame");
		lua_remove(L, -2);
		lua_pushinteger( L, id );
		lua_pushinteger( L, dtime );
		lua_call(L, 2, 0);
	} catch (exception &e) {
		LOG_ERROR( "An error occured while updating entity: ID=%d", id );
		throw;
	}
}


//
//	EGEntity::SetPose
//
void EGEntity::SetPose( const EPoint &p, const EQuaternion &q )
{
	position	=	p;
	orient		=	q;
	
	if (rigidbody.entity) {
		rigidbody.entity->SetPose( p, q );
	}
	
	if (rendmodel) {
		rendmodel->SetPose( p, q );
	}
	
	if (character.character) {
		character.character->SetPose( p, q );
	}
	
	if (ship.ship) {
		ship.ship->SetPose( p, q );
	}
}


//
//	EGEntity::GetPose
//
void EGEntity::GetPose( EPoint &p, EQuaternion &q ) const
{
	p	=	position;
	q	=	orient;
}


//
//	EGEntity::GetTable
//
void EGEntity::GetTable( lua_State *L )
{
	lua_pushinteger( L, table_ref );
	lua_gettable(L, LUA_REGISTRYINDEX);
}


//
//	EGEntity::SetTable
//
void EGEntity::SetTable( lua_State *L, int idx )
{
	lua_pushvalue(L, idx);
	table_ref = luaL_ref(L, LUA_REGISTRYINDEX);
}


/*-----------------------------------------------------------------------------
	Setup entity bahavior :
-----------------------------------------------------------------------------*/

//
//	EGEntity::Unsetup
//
void EGEntity::Unsetup( void )
{
	rigidbody.entity	=	NULL;
	rendmodel			=	NULL;
	ship.ship			=	NULL;
}


//
//	EGEntity::SetupRendModel
//
void EGEntity::SetupRendModel( const EString &path )
{
	rendmodel	=	new ERendModel( path );
	rendmodel->SetPose( position, orient );
}


//
//	EGEntity::SetupRigidbody
//
void EGEntity::SetupRigidbody( const EString &path, float mass )
{
	rigidbody.entity	=	::phys()->AddEntity();
	
	rigidbody.entity->SetDynamic();
	rigidbody.entity->SetPose( position, orient );
	rigidbody.entity->SetMesh( ge()->LoadMeshFromFile( path ) );
	rigidbody.entity->SetMass( mass );
	rigidbody.entity->Spawn();
}


//
//	EGEntity::SetupCharacter
//
void EGEntity::SetupCharacter( const EGCharDesc &char_desc )
{
	float height	=	char_desc.height;
	float radius	=	char_desc.width / 2;
	float slope		=	char_desc.climb_slope;
	float step		=	char_desc.stair_step;
	
	character.character	=	::phys()->AddCharacter( height, radius, slope, step );
	character.character->SetPose( position, orient );
}


//
//	EGEntity::CharacterMove
//
void EGEntity::CharacterMove( const EVector &dir, bool &floor, bool &ceil, bool &walls )
{
	character.character->Move( dir, floor, ceil, walls );
}


//
//	EGEntity::SetupShip
//
void EGEntity::SetupShip( lua_State *L, int tidx )
{
	ship.ship	=	new EGShip(L, tidx);
}







